package com.springcloud.xxkfz.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.springcloud.xxkfz.convert.AccountConvert;
import com.springcloud.xxkfz.service.AccountService;
import com.springcloud.xxkfz.vo.AccountVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/account")
@Log4j2
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 查询列表数据
     * @return
     */
    @GetMapping("/list")
    public List<AccountVO> list() {
        return AccountConvert.INSTANCE.domain2vo(accountService.getAccountList());
    }

    /**
     * 根据id获取数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @SentinelResource(value = "getProductById",blockHandler = "handleException")
    public AccountVO getProductById(@PathVariable("id") String id) {
        return AccountConvert.INSTANCE.toVO(accountService.getById(id));
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable("id") String id) {
        boolean isDelFlag = accountService.removeById(id);
        return isDelFlag;
    }

    public AccountVO handleException(String id, BlockException blockException) {
        log.info("flow exception{}",blockException.getClass().getCanonicalName());
        AccountVO accountVO = new AccountVO();
        accountVO.setAccountName("xxkfz");
        return  accountVO;
    }
}
